extends CharacterBody2D

var bubblegrowing = false
var netswinging = false
var bubble = null
var pointerdir = null
var dead = true
var squeakable = false
var squeaks = []
var bubbletype = 0

@onready var DIE = preload("res://2/sounds/The Death of Bubble Boy.wav")
@onready var charge = [preload("res://1/sound/Charge.wav"), preload("res://2/sounds/Charge1.wav"), preload("res://2/sounds/Charge2.wav")]
@onready var main = get_node("/root/Main")
@onready var bub = [preload("res://bubble.tscn"), preload("res://stickybubble.tscn"), preload("res://bouncingbubble.tscn"), preload("res://explodingbubble.tscn")]

const size = 1

func _ready():
	for i in range(0, 15):
		squeaks.append(load("res://1/sound/Squeak"+str(i)+".wav"))

func _input(event: InputEvent) -> void:
	if event is InputEventMouseButton and not dead:
		if event.is_pressed() and event.button_index == 1 and $BubbleGrowTimer.is_stopped():
			$CharacterSprite.animation = str(bubbletype)+"blowing"
			bubble = bub[bubbletype].instantiate()
			main.add_child(bubble)
			bubble.name += "bubble"
			$BubbleGrowTimer.start()
			bubblegrowing = true
		elif event.is_pressed() and event.button_index == 2:
			netswinging = true
			$CharacterSprite.animation = str(bubbletype)+"net"
			$Area2D.monitoring = true
		elif event.is_released():
			$CharacterSprite.animation = str(bubbletype)+"normal"
			$Area2D.monitoring = false
			bubblegrowing = false
			netswinging = false
			$BubbleGrowTimer.stop()
			if bubble != null:
				bubble.launch()
				bubble = null
			
func _physics_process(delta: float) -> void:
	if move_and_slide() == true:
		var z = get_last_slide_collision().get_collider()
		z.collide(self)
		collide(z)
	var y = (get_global_mouse_position() - global_position)
	var x = atan2(y.y, y.x)
	if bubblegrowing == true or netswinging == true:
		rotation = x
		if PI/2 > x and x >= -PI/2:
			$CharacterSprite.flip_v = false
			$Area2D/CollisionShape2D2.position.y = 15
		else:
			$CharacterSprite.flip_v = true
			$Area2D/CollisionShape2D2.position.y = -15
	if bubble != null and bubblegrowing == true:
		bubble.global_position = $Pointer.global_position
		bubble.rotation = x

func _on_bubble_grow_timer_timeout() -> void:
	if bubble != null:
		if bubble.grow() == 1:
			bubble = null
			bubblegrowing = false
		elif bubblegrowing == true:
			$AudioStreamPlayer2D.stream = charge[bubble.size-1]
			$AudioStreamPlayer2D.play()
			$BubbleGrowTimer.start()
	else:
		bubblegrowing = false

func _on_area_2d_body_entered(body: Node2D) -> void:
	if "bubble" in body.name.to_lower():
		body.collect(false)

func collide(x):
	$AudioStreamPlayer2D.stream = squeaks[randi_range(0, 14)]
	$AudioStreamPlayer2D.play()
	if "asteroid" in x.name or "debris" in x.name and dead == false:
		$AudioStreamPlayer2D.stream = DIE
		$AudioStreamPlayer2D.play()
		bubblegrowing = false
		netswinging = false
		x.velocity = (x.global_position - global_position).normalized()*200
		dead = true
		$CharacterSprite.animation = "die"
		main.die()
		$dieboom.visible = true
		$dieboom.play()

func powerup(x):
	bubbletype = x
	$PowerupTimer.start()

func _on_dieboom_animation_finished() -> void:
	$dieboom.visible = false


func _on_mouse_entered() -> void:
	squeakable = true

func _on_mouse_exited() -> void:
	squeakable = false

func _on_powerup_timer_timeout() -> void:
	bubbletype = 0
